package com.changzhi.util;

import com.changzhi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * JAVA 画图生成海报
 *
 */
@Slf4j
public class ImageUtil {

    public static String createQrCode(String h5Domain, String inviteCode){
        //二维码
        String url = h5Domain + "/#/invite/share?inviteCode=" + inviteCode;
        BufferedImage bufferedImage = ZXingUtil.orCode(url, 206*2, 206*2);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            String img = ObsUtil.upload("other/activity/invite", System.currentTimeMillis() + "_" + inviteCode, input);
            log.info("createQrCode:二维码图片链接：",img);
            return img;
        } catch (IOException e) {
            log.error("createQrCode:",e);
        }
        throw new ServiceException("请稍候重试");
    }

    /**
     * //@param str 生产的图片文字
     * //@param oldPath 原图片保存路径
     * //@param newPath 新图片保存路径
     * //@param width 定义生成图片宽度
     * //@param height 定义生成图片高度
     * @return
     * @throws IOException
     */
    public static String createPoster(String code, String qrCodeUrl){
        String str = "邀请码：" + code;
        int width = 1842;
        int height = 2378;
        try {
            Image image = ImageIO.read(new URL("https://res.xbyy.live/other/activity/invite/poster-bg-1.png"));
            //File file = new File("E:\\222.png");
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();
            g2.setBackground(Color.BLACK);
            g2.clearRect(0, 0, width, height);
            g2.drawImage(image, 0, 0, 1842, 2378, null); //这里减去25是为了防止字和图重合
            //二维码
            Image qrCodeImage = ImageIO.read(new URL(qrCodeUrl));
//            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//            String url = h5Domain + "/#/invite/share?inviteCode=" + code;
//            BufferedImage bufferedImage = ZXingUtil.orCode(url, 206*2, 206*2);
            //创建你要附加的图象。
            //小图片的路径
//            ImageIcon imgIcon = new ImageIcon("E:\\imagePath.png");
            //得到Image对象。
//            Image img = imgIcon.getImage();
            //将小图片绘到大图片上。
            //5,300 .表示你的小图片在大图片上的位置。
            g2.drawImage(qrCodeImage,350*2,534*2, null);
            /** 设置生成图片的文字样式 * */
            Font font = new Font("黑体", Font.BOLD, 60);
            g2.setFont(font);
            g2.setPaint(new Color(200, 190, 158));
            /** 设置字体在图片中的位置 在这里是居中* */
            FontRenderContext context = g2.getFontRenderContext();
            Rectangle2D bounds = font.getStringBounds(str, context);
            /** 防止生成的文字带有锯齿 * */
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            /** 在图片上生成文字 * */
            g2.drawString(str, 74*2, 80*2);
            g2.dispose();
            //boolean jpg = ImageIO.write(bi, "jpg", file);
            //图片转换base64String
            try {
                //图片转base64
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                ImageIO.write(bi,"jpg",outputStream);
                byte[] bytes = outputStream.toByteArray();
                String base64String= Base64.encodeBase64String(bytes);
                return base64String;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}